/*
  ivl_make.c  -  implementation of interval initalization functions

  This file is part of the IVL Library (LIBIVL).
  
  LIBIVL - library for interval computations 
  
  Copyright (c) 2007 Mateusz Paprocki

  The IVL Library is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  The IVL Library is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with the IVL Library; see the file LICENCE.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include "interval.h"

int ivl_make_u(ivl_ptr ivl, unsigned long x, unsigned long y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (x <= y) {
		inexact_left = mpfr_set_ui(IVL_LEFT(ivl), x, GMP_RNDD);
		inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), y, GMP_RNDU);
	} else {
		inexact_left = mpfr_set_ui(IVL_LEFT(ivl), y, GMP_RNDD);
		inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), x, GMP_RNDU);
	}
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);

	return inexact;
}

int ivl_make_s(ivl_ptr ivl, long x, long y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (x <= y) {
		inexact_left = mpfr_set_si(IVL_LEFT(ivl), x, GMP_RNDD);
		inexact_right = mpfr_set_si(IVL_RIGHT(ivl), y, GMP_RNDU);
	} else {
		inexact_left = mpfr_set_si(IVL_LEFT(ivl), y, GMP_RNDD);
		inexact_right = mpfr_set_si(IVL_RIGHT(ivl), x, GMP_RNDU);
	}
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);
	
	return inexact;
}

int ivl_make_d(ivl_ptr ivl, double x, double y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (x <= y) {
		inexact_left = mpfr_set_d(IVL_LEFT(ivl), x, GMP_RNDD);
		inexact_right = mpfr_set_d(IVL_RIGHT(ivl), y, GMP_RNDU);
	} else {
		inexact_left = mpfr_set_d(IVL_LEFT(ivl), y, GMP_RNDD);
		inexact_right = mpfr_set_d(IVL_RIGHT(ivl), x, GMP_RNDU);
	}
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);
		
	return inexact;	
}

int ivl_make_z(ivl_ptr ivl, mpz_srcptr x, mpz_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (mpz_cmp(x, y) <= 0) {
		inexact_left = mpfr_set_z(IVL_LEFT(ivl), x, GMP_RNDD);
		inexact_right = mpfr_set_z(IVL_RIGHT(ivl), y, GMP_RNDU);
	} else {
		inexact_left = mpfr_set_z(IVL_LEFT(ivl), y, GMP_RNDD);
		inexact_right = mpfr_set_z(IVL_RIGHT(ivl), x, GMP_RNDU);
	}
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);
		
	return inexact;
}

int ivl_make_q(ivl_ptr ivl, mpq_srcptr x, mpq_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (mpq_cmp(x, y) <= 0) {
		inexact_left = mpfr_set_q(IVL_LEFT(ivl), x, GMP_RNDD);
		inexact_right = mpfr_set_q(IVL_RIGHT(ivl), y, GMP_RNDU);
	} else {
		inexact_left = mpfr_set_q(IVL_LEFT(ivl), y, GMP_RNDD);
		inexact_right = mpfr_set_q(IVL_RIGHT(ivl), x, GMP_RNDU);
	}
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);
		
	return inexact;	
}

int ivl_make_f(ivl_ptr ivl, mpfr_srcptr x, mpfr_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (mpfr_lessequal_p(x, y)) {
		inexact_left = mpfr_set(IVL_LEFT(ivl), x, GMP_RNDD);
		inexact_right = mpfr_set(IVL_RIGHT(ivl), y, GMP_RNDU);
	} else {
		inexact_left = mpfr_set(IVL_LEFT(ivl), y, GMP_RNDD);
		inexact_right = mpfr_set(IVL_RIGHT(ivl), x, GMP_RNDU);
	}
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);
		
	return inexact;	
}

int ivl_make_str(ivl_ptr ivl, const char *x, const char *y, int base) {
	int inexact = 0, inexact_left, inexact_right;

	inexact_left = mpfr_set_str(IVL_LEFT(ivl), x, base, GMP_RNDD);
	inexact_right = mpfr_set_str(IVL_RIGHT(ivl), y, base, GMP_RNDU);
	
	if ((inexact_left == -1) || (inexact_right == -1)) {
		ivl->domain = IVL_UNDEFINED;
		inexact = -1;
	} else {
		if (mpfr_less_p(IVL_RIGHT(ivl), IVL_LEFT(ivl))) {
			/* if bounds were set in an invalid order we can't just swap them
			   but we have to compute them twice, because rouding has changed */
			inexact_left = mpfr_set_str(IVL_LEFT(ivl), y, base, GMP_RNDD);
			inexact_right = mpfr_set_str(IVL_RIGHT(ivl), x, base, GMP_RNDU);
		}
		
		IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);
	}

	return inexact;
}

void ivl_make_unbounded(ivl_ptr ivl, ivl_domain_t dom) {
	mpfr_set_inf(IVL_LEFT(ivl), -1);
	mpfr_set_inf(IVL_RIGHT(ivl), 1);

	ivl->domain = dom;
}
